package com.rasenhund.android.gsimulator.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rasenhund.android.gsimulator.R;
import com.rasenhund.android.gsimulator.common.Const;
import com.rasenhund.android.gsimulator.common.Const.CATEGORY;
import com.rasenhund.android.gsimulator.schema.MobileSuit;

/**
 * コストシミュレート画面
 */
public class CostSimulateActivity extends BaseActivity {
    @SuppressWarnings("unused")
    private static final String TAG = CostSimulateActivity.class.getSimpleName();
    private final CostSimulateActivity self = this;

    TextView mTxtMsCost;
    TextView mTxtBaseCost;
    TextView mTxtRemainCost;

    RelativeLayout mBaseLayout; // 拠点

    int miCostTotal = 0;
    int miMsCostTotal = 0;
    int miBaseCostTotal = 0;

    private final String MODE_BASE = "0"; // 拠点
    private final String MODE_MS = "1"; // MS

    private Handler mHandler = null; // UI操作用のハンドラ
    private View mView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cost_simulate);

        // 広告エリアの初期化
        initAdView();

        mTxtMsCost = (TextView) findViewById(R.id.txtMsCost);
        mTxtBaseCost = (TextView) findViewById(R.id.txtBaseCost);
        mTxtRemainCost = (TextView) findViewById(R.id.txtRemainCost);

        mBaseLayout = (RelativeLayout) findViewById(R.id.layout_base);
        mBaseLayout.setOnClickListener(slot_OnClickListener);
        mBaseLayout.setOnLongClickListener(slot_OnLongClickListener);

        // 拠点コストもMSオブジェクトを流用する
        MobileSuit base = new MobileSuit();
        base.setMode(MODE_BASE);
        base.setCost(mBaseCost);
        base.setMsName("拠点");
        base.setCategoryCd(CATEGORY.BASE);
        setSlot(mBaseLayout, base);

        // MSスロット1～8の初期設定
        for (int i = 0; i < mSlotIdList.length; i++) {

            RelativeLayout slot = (RelativeLayout) findViewById(mSlotIdList[i]);
            slot.setClickable(true);

            slot.setOnClickListener(slot_OnClickListener);
            slot.setOnLongClickListener(slot_OnLongClickListener);

            MobileSuit item = mLstMachine.get(i);
            item.setMode(MODE_MS);
            setSlot(slot, item);

        }

        // UI操作用のハンドラを生成
        mHandler = new Handler();

    }

    @Override
    protected void onResume() {
        super.onResume();

        // ガイド用のメッセージを表示
        Toast.makeText(self, getString(R.string.message_costsimulate_guide), Toast.LENGTH_SHORT)
                .show();

        setCost();

    }

    /********************
     * EventListener
     ********************/
    
    /**
     * MSスロットクリック時の処理
     */
    private View.OnClickListener slot_OnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mView = v;

            // UI操作はメインスレッドのみで行うためハンドラに投げる
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    // 被撃墜数増加
                    addDownCount(mView);
                }
            });

        }

    };

    /**
     * MSスロットロングクリック時の処理
     */
    private View.OnLongClickListener slot_OnLongClickListener = new View.OnLongClickListener() {
        
        @Override
        public boolean onLongClick(View v) {

            mView = v;

            // UI操作はメインスレッドのみで行うためハンドラに投げる
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    // 被撃墜数減少
                    redDownCount(mView);
                }
            });

            return true;
        }
    };

    /********************
     * Method
     ********************/
    /**
     * Viewにアイテムをセット
     * 
     * @param view
     * @param item
     */
    private void setSlot(View view, MobileSuit item) {

        if (item == null || item.getCategoryCd() < 0) {
            view.setVisibility(View.GONE);
        }

        TextView txtCategory = (TextView) view.findViewById(R.id.txtCategory);
        TextView txtModelNo = (TextView) view.findViewById(R.id.txtModelNo);
        TextView txtMsName = (TextView) view.findViewById(R.id.txtMsName);
        TextView txtCost = (TextView) view.findViewById(R.id.txtCost);
        TextView txtDown = (TextView) view.findViewById(R.id.txtDown);
        TextView hidMode = (TextView) view.findViewById(R.id.hidMode);

        // カテゴリ
        switch (item.getCategoryCd()) {
            case Const.CATEGORY.GRAPP:
                txtCategory.setText(getString(R.string.category_grapp));
                txtCategory.setBackgroundResource(R.drawable.category_grapp);
                break;
            case Const.CATEGORY.SHORT:
                txtCategory.setText(getString(R.string.category_short));
                txtCategory.setBackgroundResource(R.drawable.category_short);
                break;
            case Const.CATEGORY.SHOOT:
                txtCategory.setText(getString(R.string.category_shoot));
                txtCategory.setBackgroundResource(R.drawable.category_shoot);
                break;
            case Const.CATEGORY.TANK:
                txtCategory.setText(getString(R.string.category_tank));
                txtCategory.setBackgroundResource(R.drawable.category_tank);
                break;
            case Const.CATEGORY.SNIPE:
                txtCategory.setText(getString(R.string.category_snipe));
                txtCategory.setBackgroundResource(R.drawable.category_snipe);
                break;
            default:
                txtCategory.setText("-");
                txtCategory.setBackgroundResource(R.drawable.normal);
                break;
        }

        // 型番
        txtModelNo.setText(item.getModelNo());
        // MS名
        txtMsName.setText(item.getMsName());
        // コスト
        txtCost.setText(String.valueOf(item.getCost()));
        // 被害撃墜数
        txtDown.setText(String.valueOf(0));
        // モード
        hidMode.setText(item.getMode());

    }
    
    /**
     * コストの取得
     * @param view
     * @return
     */
    private int getCost(View view){
        
        TextView textCost = (TextView) view.findViewById(R.id.txtCost);
        int iCost = Integer.valueOf(textCost.getText().toString());
        
        return iCost;
        
    }

    /**
     * 被撃墜数増加
     */
    private void addDownCount(View view) {

        // エラー回避
        if (mView == null) {
            return;
        }

        TextView textDown = (TextView) view.findViewById(R.id.txtDown);
        TextView hidMode = (TextView) view.findViewById(R.id.hidMode);

        int iCost = getCost(view);
        int count = Integer.valueOf(textDown.getText().toString());
        String mode = hidMode.getText().toString();

        if (mode.equals(MODE_BASE)) {
            miBaseCostTotal = miBaseCostTotal + iCost;
        } else if (mode.equals(MODE_MS)) {
            miMsCostTotal = miMsCostTotal + iCost;
        }

        count++;
        textDown.setText(String.valueOf(count));

        setCost();

        // 処理が完了したので空にしておく
        mView = null;

        return;

    }

    /**
     * 被撃墜数減少
     * 
     * @param view
     */
    private void redDownCount(View view) {

        TextView textDown = (TextView) view.findViewById(R.id.txtDown);
        TextView hidMode = (TextView) view.findViewById(R.id.hidMode);

        int iCost = getCost(view);
        int count = Integer.valueOf(textDown.getText().toString());
        String mode = hidMode.getText().toString();

        // 被撃墜数は0以下にはしない
        if (count > 0) {

            if (mode.equals(MODE_BASE)) {
                miBaseCostTotal = miBaseCostTotal - iCost;
            } else if (mode.equals(MODE_MS)) {
                miMsCostTotal = miMsCostTotal - iCost;
            }

            count--;
            textDown.setText(String.valueOf(count));

        }

        setCost();

        return;

    }

    /**
     * コストの再表示
     */
    private void setCost() {

        // 残コストの計算
        int iRemainCost = 0;
        iRemainCost = mTotalCost - miBaseCostTotal - miMsCostTotal;

        mTxtBaseCost.setText(String.valueOf(miBaseCostTotal));
        mTxtMsCost.setText(String.valueOf(miMsCostTotal));
        mTxtRemainCost.setText(String.valueOf(iRemainCost));

    }

}
